<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017/11/24
 * Time: 14:00
 */

namespace app\admin\model;

use think\Model;

class SettlementRecord extends Model
{

    public function getAll(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['lesson.lesson_name|lesson.id'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $data = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user','lesson.teacher_id=user.id','LEFT')
            ->where($where)
            ->where("settlement.is_check=0")
            ->field('settlement.id,settlement.lesson_id,lesson_name,lesson.teacher_id,user.full_name as teacher_name,
                    teacher_total_cost,is_check,is_grant')
            ->select();

        if($data){
            foreach($data as $k => $v){
                $data[$k] = $v->toArray();
            }
        }
        return $data;
    }


    public function getCount(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['lesson.lesson_name|lesson.id'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $count = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user','lesson.teacher_id=user.id','LEFT')
            ->where($where)
            ->where("settlement.is_check=0")
            ->field('settlement.id,settlement.lesson_id,lesson_name,user.full_name as teacher_name,
                    teacher_total_cost,is_check,is_grant')
            ->count();
        return $count;
    }

    public function getOne($id){
        $where = "settlement.id=".$id;
        $data = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user teacher_user','settlement.teacher_user=teacher_user.id','LEFT')
            ->join('user education_user','settlement.education_user = education_user.id','LEFT')
            ->join('user reclass_user','settlement.reclass_user = reclass_user.id','LEFT')
            ->join('user classroom_owner_user','settlement.classroom_owner_user = classroom_owner_user.id','LEFT')
            ->join('user material_user','settlement.material_user = material_user.id','LEFT')
            ->join('user total_rec_user','settlement.total_rec_user = total_rec_user.id','LEFT')
            ->join('user province_rec_user','settlement.province_rec_user = province_rec_user.id','LEFT')
            ->join('user city_rec_user','settlement.city_rec_user = city_rec_user.id','LEFT')
            ->join('user area_rec_user','settlement.area_rec_user = area_rec_user.id','LEFT')
            ->join('user school_rec_user','settlement.school_rec_user = school_rec_user.id','LEFT')
            ->join('user cooperate_user','settlement.cooperate_user = cooperate_user.id','LEFT')
            ->join('user special_user1','settlement.special_user1 = special_user1.id','LEFT')
            ->join('user special_user2','settlement.special_user2 = special_user2.id','LEFT')
            ->join('user special_user3','settlement.special_user3 = special_user3.id','LEFT')
            ->join('user special_user4','settlement.special_user4 = special_user4.id','LEFT')
            ->join('user special_user5','settlement.special_user5 = special_user5.id','LEFT')

            ->where($where)
            ->field('settlement.id,lesson_name,lesson.teacher_id,
                    rec_student_total_cost,rec_student_cost_count,
                    rec_teacher_total_cost,rec_teacher_cost_count,
                    rec_classroom_total_cost,rec_classroom_cost_count,
                    teacher_user.full_name as teacher_user,teacher_total_cost,teacher_cost_count,
                    student_total_cost,student_cost_count,
                    education_user.full_name as education_user,education_total_cost,education_cost_count,
                    reclass_user.full_name as reclass_user,reclass_total_cost,reclass_cost_count,
                    classroom_owner_user.full_name as classroom_owner_user,classroom_owner_user,classroom_owner_total_cost,classroom_owner_cost_count,
                    material_user.full_name as material_user,material_total_cost,material_cost_count,
                    total_rec_user.full_name as total_rec_user,total_rec_total_cost,total_rec_cost_count,
                    province_rec_user.full_name as province_rec_user,province_rec_total_cost,province_rec_cost_count,
                    city_rec_user.full_name as city_rec_user,city_rec_total_cost,city_rec_cost_count,
                    area_rec_user.full_name as area_rec_user,area_rec_total_cost,area_rec_cost_count,
                    school_rec_user.full_name as school_rec_user,school_rec_total_cost,school_rec_cost_count,
                    cooperate_user.full_name as cooperate_user,cooperate_total_cost,cooperate_cost_count,
                    special_user1.full_name as special_user1,special_user1_total_cost,special_user1_cost_count,
                    special_user2.full_name as special_user2,special_user2_total_cost,special_user2_cost_count,
                    special_user3.full_name as special_user3,special_user3_total_cost,special_user3_cost_count,
                    special_user4.full_name as special_user4,special_user4_total_cost,special_user4_cost_count,
                    special_user5.full_name as special_user5,special_user5_total_cost,special_user5_cost_count,
                    settlement_lesson_num_count,settlement_escape_lesson_num,settlement_escape_lesson_num_count,
                    is_check,is_grant')
            ->find();
        if($data){
            $data = $data->toArray();
        }
        return $data;
    }

    public function getAllHadSettlement(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['lesson.lesson_name|lesson.id'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $data = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user','lesson.teacher_id=user.id','LEFT')
            ->where($where)
            ->where("settlement.is_check=1")
            ->field('settlement.id,settlement.lesson_id,lesson_name,lesson.teacher_id,user.full_name as teacher_name,
                    teacher_total_cost,is_check,is_grant')
            ->select();

        if($data){
            foreach($data as $k => $v){
                $data[$k] = $v->toArray();
            }
        }
        return $data;
    }


    public function getCountHadSettlement(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['lesson.lesson_name|lesson.id'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $count = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user','lesson.teacher_id=user.id','LEFT')
            ->where($where)
            ->where("settlement.is_check=1")
            ->field('settlement.id,settlement.lesson_id,lesson_name,user.full_name as teacher_name,
                    teacher_total_cost,is_check,is_grant')
            ->count();
        return $count;
    }



    public function getOneHadSettlement($id){
        $where = "settlement.id=".$id;
        $data = $this
            ->alias("settlement")
            ->join("lesson",'settlement.lesson_id=lesson.id','LEFT')
            ->join('user teacher_user','settlement.teacher_user=teacher_user.id','LEFT')
            ->join('user education_user','settlement.education_user = education_user.id','LEFT')
            ->join('user reclass_user','settlement.reclass_user = reclass_user.id','LEFT')
            ->join('user classroom_owner_user','settlement.classroom_owner_user = classroom_owner_user.id','LEFT')
            ->join('user material_user','settlement.material_user = material_user.id','LEFT')
            ->join('user total_rec_user','settlement.total_rec_user = total_rec_user.id','LEFT')
            ->join('user province_rec_user','settlement.province_rec_user = province_rec_user.id','LEFT')
            ->join('user city_rec_user','settlement.city_rec_user = city_rec_user.id','LEFT')
            ->join('user area_rec_user','settlement.area_rec_user = area_rec_user.id','LEFT')
            ->join('user school_rec_user','settlement.school_rec_user = school_rec_user.id','LEFT')
            ->join('user cooperate_user','settlement.cooperate_user = cooperate_user.id','LEFT')
            ->join('user special_user1','settlement.special_user1 = special_user1.id','LEFT')
            ->join('user special_user2','settlement.special_user2 = special_user2.id','LEFT')
            ->join('user special_user3','settlement.special_user3 = special_user3.id','LEFT')
            ->join('user special_user4','settlement.special_user4 = special_user4.id','LEFT')
            ->join('user special_user5','settlement.special_user5 = special_user5.id','LEFT')

            ->where($where)
            ->field('settlement.id,settlement.lesson_id,lesson_name,
                    lesson.teacher_id,lesson.com_lesson_num,
                    rec_student_total_cost,rec_student_cost_count,
                    rec_teacher_total_cost,rec_teacher_cost_count,
                    rec_classroom_total_cost,rec_classroom_cost_count,
                    teacher_user.full_name as teacher_user,teacher_total_cost,teacher_cost_count,
                    student_total_cost,student_cost_count,
                    education_user.full_name as education_user,education_total_cost,education_cost_count,
                    reclass_user.full_name as reclass_user,reclass_total_cost,reclass_cost_count,
                    classroom_owner_user.full_name as classroom_owner_user,classroom_owner_user,classroom_owner_total_cost,classroom_owner_cost_count,
                    material_user.full_name as material_user,material_total_cost,material_cost_count,
                    total_rec_user.full_name as total_rec_user,total_rec_total_cost,total_rec_cost_count,
                    province_rec_user.full_name as province_rec_user,province_rec_total_cost,province_rec_cost_count,
                    city_rec_user.full_name as city_rec_user,city_rec_total_cost,city_rec_cost_count,
                    area_rec_user.full_name as area_rec_user,area_rec_total_cost,area_rec_cost_count,
                    school_rec_user.full_name as school_rec_user,school_rec_total_cost,school_rec_cost_count,
                    cooperate_user.full_name as cooperate_user,cooperate_total_cost,cooperate_cost_count,
                    special_user1.full_name as special_user1,special_user1_total_cost,special_user1_cost_count,
                    special_user2.full_name as special_user2,special_user2_total_cost,special_user2_cost_count,
                    special_user3.full_name as special_user3,special_user3_total_cost,special_user3_cost_count,
                    special_user4.full_name as special_user4,special_user4_total_cost,special_user4_cost_count,
                    special_user5.full_name as special_user5,special_user5_total_cost,special_user5_cost_count,
                    settlement_lesson_num,settlement_lesson_num_count,
                    settlement_escape_lesson_num,settlement_escape_lesson_num_count,
                    is_check,is_grant')
            ->find();
        if($data){
            $data = $data->toArray();
        }
        return $data;
    }
}